package com.example.servicearea.dahua;

import com.example.servicearea.lib.NetSDKLib;
import com.example.servicearea.utils.StringUtil;
import com.example.servicearea.utils.VehicleTypeTransform;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Date;

public class PersonCapture {
    private static String dbUrl = "jdbc:mysql://localhost:3306/service?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8"; // 获取连接
    private static String dbUser = "root";
    private static String dbPassword= "Root@123456!";

    private  String deviceIp;
    private  Integer deviceId;
    private Integer port;
    private String deviceType;
    private NetSDKLib.LLong loginHandle=new NetSDKLib.LLong(0);
    private NetSDKLib.LLong statHandle=new NetSDKLib.LLong(0);
    private String user;
    private String pwd;

    private Date lastTime = null;
    private  Date currentTime = null;
    private  Integer lastEnterNum = null;
    private  Integer lastExitNum = null;
    private  Integer currentEnterNum = null;
    private  Integer currentExitNum = null;
    public PersonCapture(Integer deviceId, String deviceIp, Integer port, String user, String pwd ,String deviceType){
        this.deviceId=deviceId;
        this.deviceIp=deviceIp;
        this.user=user;
        this.pwd=pwd;
        this.port=port;
        this.deviceType=deviceType;
    }
    public  String getDeviceIp() {
        return deviceIp;
    }
    public  Integer getPort() {
        return port;
    }
    public  void setDeviceIp(String deviceIp) {
        deviceIp = deviceIp;
    }
    public  Integer getDeviceId() {
        return deviceId;
    }
    public  void setDeviceId(Integer deviceId) {
        deviceId = deviceId;
    }
    public  String getUser() {
        return user;
    }
    public  String getPwd() {
        return pwd;
    }
    public  String getDeviceType(){return deviceType;}
    public  void setLoginHandle(NetSDKLib.LLong handle) {
        loginHandle = handle;
    }
    public  void setStatHandle(NetSDKLib.LLong handle) {
        statHandle = handle;
    }
    public NetSDKLib.LLong getStatHandle(){
        return statHandle;
    }
    public void printInfo(){
        System.out.println("id="+deviceId);
        System.out.println("ip="+deviceIp);
        System.out.println("user="+user);
        System.out.println("pwd="+pwd);
        System.out.println("port="+port);
        System.out.println("type="+deviceType);
    }
    public void savePersonData(NetSDKLib.NET_VIDEOSTAT_SUMMARY stVideoState){
        String time = StringUtil.clearStr(stVideoState.stuTime.toStringTime());
        Date date = stVideoState.stuTime.toDate();

        if (lastTime == null) {
            lastTime = date;
            lastEnterNum = stVideoState.stuEnteredSubtotal.nTotal;
            lastExitNum = stVideoState.stuExitedSubtotal.nTotal;
        } else {
            currentEnterNum = stVideoState.stuEnteredSubtotal.nTotal;
            currentExitNum = stVideoState.stuExitedSubtotal.nTotal;
            currentTime = date;
            //比较一下时间
            long diff, min;
            diff = min = 0;
            diff = currentTime.getTime() - lastTime.getTime();
            min = diff / 1000 ;
            //System.out.println(min);
            if (min >= 5 * 60) {
                int EnterNum = currentEnterNum - lastEnterNum;
                //System.out.println(EnterNum);
                int ExitNum = currentExitNum - lastExitNum;
                //System.out.println(ExitNum);
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");             // 加载驱动类
                    Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
                    String sql = "insert into peopleflow(device_ip,send_time,device_id,enter_num,exit_num) " +
                            "values('" + getDeviceIp() + "','" + time + "','" + getDeviceId() + "','" + EnterNum + "','" + ExitNum + "')";  //******
                    // 根据页面的数据，生成插入学生的sql语句

                    Statement stmt = conn.createStatement();// 创建statement
                    int count = stmt.executeUpdate(sql);// 执行sql


                    conn.close();
                    stmt.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                lastTime = currentTime;
                lastEnterNum = currentEnterNum;
                lastExitNum = currentExitNum;
            }
        }
    }

    public void saveVehicleData(String plateNumber, String vehicleTime ,Integer vehicleType){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");             // 加载驱动类
            Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
            String sql = "insert into vehicle(device_ip,send_time,license,device_id,vehicle_type) " +
                    "values( '"+ getDeviceIp() +"'," +
                    "'"+ vehicleTime +"'," +
                    "'"+ plateNumber +"'," +
                    "'"+ getDeviceId() +"'," +
                    "'"+ vehicleType +"'" +
                    ")";

            Statement stmt = conn.createStatement();// 创建statement
            int count = stmt.executeUpdate(sql);// 执行sql
            conn.close();
            stmt.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }


}
